package com.woniuxy.dao;

import com.woniuxy.entity.Account;
import com.woniuxy.entity.UserInfo;
import com.woniuxy.utils.ConnectionUtil;

import java.sql.*;

public class AccountDao {
    private Connection connection = ConnectionUtil.getConnection();

    public void regiestAccount(Account account) {
        //注册账号信息 交给同学们完成
        String sql = "insert into account (username,password,userid,createtime) values (?,?,?,NOW())";
        Connection connection = ConnectionUtil.getConnection();
        try {
            //预编译
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            //对应每个问号，放入对应的值
            preparedStatement.setString(1,account.getUsername());
            preparedStatement.setString(2, account.getPassword());
            preparedStatement.setInt(3, account.getUserid());
            //执行这个预编译sql
            preparedStatement.executeUpdate();
            System.out.println("账号密码创建成功！");
        } catch (SQLException throwables) {
            System.out.println("账号密码创建失败，原因是"+throwables.getMessage());
            throwables.printStackTrace();
        }finally {
            try {
                //了解关闭数据库连接的行为即可，后期由框架底层自动帮忙实现
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }

    /**
     * 作为一个查询方法，来判断你要创建的账号名 是否有重复
     * @param account
     */
    public int regiestCheckNumber(Account account) {
        String sql = "select count(*) from account where username = ?";
        Connection connection = ConnectionUtil.getConnection();
        ResultSet resultSet = null;
        int number = 0;
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, account.getUsername());
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                //对应查询出的数量
                String num = resultSet.getString(1);
                number = Integer.parseInt(num);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return number;
    }

    /**
     * 进行登录用
     * @param username
     * @param password
     */
    public void login(String username, String password) {
        String sql = "select u.* from userinfo u LEFT JOIN account a on u.id=a.userid where username = ? and password = ?";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
//                ResultSetMetaData metaData = resultSet.getMetaData();//元数据来获取字段总数
//                int columnCount = metaData.getColumnCount();//字段总数
                    Account.getUserInfo().setId(resultSet.getInt("id"));
                    Account.getUserInfo().setName(resultSet.getString("name"));
                    Account.getUserInfo().setAge(resultSet.getInt("age"));
                    Account.getUserInfo().setCollege(resultSet.getString("college"));
                    Account.getUserInfo().setSex(resultSet.getString("sex"));
                    Account.getUserInfo().setSpecialty(resultSet.getString("specialty"));
                    Account.getUserInfo().setStuid(resultSet.getString("stuid"));
                }
            } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
